//
//  TimeSelectorVC.m
//  YueYueSpecailCarDriver
//
//  Created by 李亚军 on 17/2/20.
//  Copyright © 2017年 黄旺鑫. All rights reserved.
//

#import "TimeSelectorVC.h"

@interface TimeSelectorVC ()<UIPickerViewDelegate , UIPickerViewDataSource>


/**
 弹出试图
 */
@property (weak, nonatomic) IBOutlet UIView *popView;





@end

@implementation TimeSelectorVC

/**
 初始化时间选择器
 */
+ (TimeSelectorVC *)initTimeSelectorVC {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Orders" bundle:[NSBundle mainBundle]];
    
    TimeSelectorVC *timeSelectorVC = [storyboard instantiateViewControllerWithIdentifier:@"TimeSelectorVCID"];
    
    return timeSelectorVC;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    [self configureDefaultAttributes];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self configureDefaultAttributes];
}





/**
 配置默认时间
 */
- (void)configureDefaultAttributes {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow.rootViewController addChildViewController:self];
    [keyWindow.rootViewController.view addSubview:self.view];
    
    
    self.view.alpha = 0.0;
    self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 0.5);
    
    
    self.popView.layer.cornerRadius = 5.0;
    self.popView.layer.masksToBounds = YES;
    self.popView.backgroundColor = [UIColor whiteColor];
    
}

- (void)show {
    
    [UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        
        self.view.alpha = 1.0;
        self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    } completion:^(BOOL finished) {
        
        
    }];
}


- (void)dismiss {
    
    [UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        
        self.view.alpha = 0.0;
        self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    } completion:^(BOOL finished) {
        
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

#pragma mark - Action
- (IBAction)confirmAction:(UIButton *)sender {
    
    if (self.confirmActionBlock) {
        
        self.confirmActionBlock();
    }
    
    [self dismiss];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    if (self.dataSource == nil) {
        return 0;
    }
    
    id arr = self.dataSource.firstObject;
    
    if ([arr isKindOfClass:[NSArray class]]) { // 如果第一个的数据是一个数组
        
        return self.dataSource.count;
    }else {
        
        return 1;
    }
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    id arr = self.dataSource.firstObject;
    
    if ([arr isKindOfClass:[NSArray class]]) { // 如果第一个的数据是一个数组
        
        NSArray *titleArray = self.dataSource[component];
        
        return [titleArray count];
    }else {
        
        return self.dataSource.count;
    }
}

#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return 50;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 32;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    id arr = self.dataSource.firstObject;
    
    if ([arr isKindOfClass:[NSArray class]]) { // 如果第一个的数据是一个数组
        
        NSArray *titleArray = self.dataSource[component];
        return titleArray[row];
    }else {
        
        return self.dataSource[row];
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    UIView *titleView = [UIView new];
    titleView.frame = CGRectMake(0, 0, 50, 32);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorWithRed:107 / 255.0 green:120 / 255.0 blue:134 / 255.0 alpha:1.0];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    
    id arr = self.dataSource.firstObject;
    if ([arr isKindOfClass:[NSArray class]]) {
        
        NSArray *titleArray = self.dataSource[component];
        titleLabel.text = titleArray[row];
    }else {
        
        titleLabel.text = self.dataSource[row];
    }
    
    
    [titleView addSubview:titleLabel];
    
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
    
    return titleView;
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
    
    if (self.delegate) {

        [self.delegate timeSelectorVC:self pickView:pickerView didSelectRow:row inComponent:component];
    }
    
    
}


#pragma mark - dealloc
- (void)dealloc
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
